package com.blb.ioc.demo2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 计算机
 * @Component 注解会被IOC容器扫描到
 */
@Component
public class Computer {

    //品牌 @Value 注入基本类型和字符串
    @Value("华为")
    private String brand;
    //cpu @Autowired 通过类型查找IOC容器的对象，注入到属性中
    @Qualifier("intel")
    @Autowired
    private Cpu cpu;
    //内存 @Resource 通过对象名查找，查不到再按类型找
    @Resource
    private Memory memory;

    public Computer() {
    }

    public Computer(String brand, Cpu cpu, Memory memory) {
        System.out.println("执行有参构造方法");
        this.brand = brand;
        this.cpu = cpu;
        this.memory = memory;
    }

    public Cpu getCpu() {
        return cpu;
    }

    public void setCpu(Cpu cpu)
    {
        System.out.println("执行了setCpu");
        this.cpu = cpu;
    }

    public Memory getMemory() {
        return memory;
    }

    public void setMemory(Memory memory) {
        this.memory = memory;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    /**
     * 启动
     */
    public void start(){
        System.out.println(brand + "电脑启动了");
        cpu.run();
        memory.read();
        memory.write();
    }
}
